package com.bbzn.device.client.controller;


import com.bbzn.device.client.vo.req.VcrInfoVO;
import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.constant.DeviceConstant;
import com.bbzn.device.client.constant.VcrInfoConstant;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.dataobject.VcrInfo;
import com.bbzn.device.client.dto.api.qiniu.DelFileRequest;
import com.bbzn.device.client.dto.vcr.VcrInfoDTO;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.CommonService;
import com.bbzn.device.client.service.QiniuOssApiService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.service.VcrInfoService;
import com.bbzn.device.client.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/vcr")
//@CrossOrigin
public class VcrInfoController extends BaseApiService {

    @Autowired
    private CommonService commonService;

    @Autowired
    private VcrInfoService vcrInfoService;

    @Autowired
    private QiniuOssApiService qiniuOssApiService;

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @RequestMapping(value = "/getVcrList", method = RequestMethod.POST)
    @OperationAnnotation(content = "文件列表分页查询")
    public BaseResponse getVcrList(@RequestBody VcrInfoVO reqVO , HttpServletRequest request) throws Exception {
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        VcrInfoDTO vcrInfoDTO = new VcrInfoDTO();
        vcrInfoDTO.setUserId(userId);
        vcrInfoDTO.setOperatorId(userId);
        vcrInfoDTO.setPageNum(reqVO.getPageNum());
        vcrInfoDTO.setPageSize(reqVO.getPageSize());
        if (null != reqVO.getPageNum()) {
            vcrInfoDTO.setPageNum(reqVO.getPageNum());
        }
        if (null != reqVO.getPageSize()) {
            vcrInfoDTO.setPageSize(reqVO.getPageSize());
        }
        //模糊查询
        if (null != reqVO.getImei()) {
            vcrInfoDTO.setImei(reqVO.getImei());
        }
        if (null != reqVO.getBucketName()) {
            vcrInfoDTO.setBucketName(reqVO.getBucketName());
        }
        if (null != reqVO.getFileName()) {
            vcrInfoDTO.setFileName(reqVO.getFileName());
        }
        if (null != reqVO.getType()) {
            vcrInfoDTO.setType(reqVO.getType());
        }
        vcrInfoDTO.setStartIndex((reqVO.getPageNum() - 1) * reqVO.getPageSize());
        PageInfo<VcrInfo> vcrInfos = vcrInfoService.selectByPage(vcrInfoDTO);
        return setResultSuccess(vcrInfos);
    }

    @RequestMapping(value = "/addVcrInfo", method = RequestMethod.POST)
    @OperationAnnotation(content = "文件新增记录")
    public BaseResponse addVcrInfo(@RequestBody VcrInfoVO reqVO , HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        String userName =  MayiktJwtUtils.getUsername(request, secretKey);
        if (CollectionUtils.isEmpty(reqVO.getImeis())) {
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getBucketName()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(VcrInfoConstant.VCRINFO_ENUM, VcrInfoConstant.VCRINFO_NO_BUCKKETNAME_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getFileName()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(VcrInfoConstant.VCRINFO_ENUM, VcrInfoConstant.VCRINFO_NO_FILENAME_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getType()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(VcrInfoConstant.VCRINFO_ENUM, VcrInfoConstant.VCRINFO_NO_TYPE_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }
        reqVO.getImeis().stream().forEach(
                imei -> {
                    VcrInfo vcrInfo = new VcrInfo();
                    vcrInfo.setUserId(userId);
                    vcrInfo.setImei(imei);
                    vcrInfo.setBucketName(reqVO.getBucketName());
                    vcrInfo.setFileName(reqVO.getFileName());
                    vcrInfo.setType(reqVO.getType());
                    vcrInfo.setCreateTime(new Date());
                    vcrInfo.setCreateBy(userName);
                    vcrInfoService.insertSelective(vcrInfo);
                }
        );
        systemParam = systemParamByCacheService.getValues(VcrInfoConstant.VCRINFO_ENUM, VcrInfoConstant.VCRINFO_ADD_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    @RequestMapping(value = "/updateVcrInfo", method = RequestMethod.POST)
    @OperationAnnotation(content = "文件修改记录")
    public BaseResponse updateVcrInfo(@RequestBody VcrInfoVO reqVO , HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        String userName =  MayiktJwtUtils.getUsername(request, secretKey);
        if (!OptionalBean.ofNullable(reqVO.getId()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(VcrInfoConstant.VCRINFO_ENUM, VcrInfoConstant.VCRINFO_NO_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getBucketName()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(VcrInfoConstant.VCRINFO_ENUM, VcrInfoConstant.VCRINFO_NO_BUCKKETNAME_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getFileName()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(VcrInfoConstant.VCRINFO_ENUM, VcrInfoConstant.VCRINFO_NO_FILENAME_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(reqVO.getType()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(VcrInfoConstant.VCRINFO_ENUM, VcrInfoConstant.VCRINFO_NO_TYPE_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }
        VcrInfo vcrInfo = new VcrInfo();
        vcrInfo.setId(reqVO.getId());
        vcrInfo.setBucketName(reqVO.getBucketName());
        vcrInfo.setFileName(reqVO.getFileName());
        vcrInfo.setType(reqVO.getType());
        vcrInfo.setUpdateTime(new Date());
        vcrInfo.setUpdateBy(userName);;
        vcrInfoService.updateByPrimaryKey(vcrInfo);
        systemParam = systemParamByCacheService.getValues(VcrInfoConstant.VCRINFO_ENUM, VcrInfoConstant.VCRINFO_UPDATE_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    @RequestMapping(value = "/delVcrInfo", method = RequestMethod.POST)
    @OperationAnnotation(content = "文件删除")
    public BaseResponse delVcrInfo(@RequestBody VcrInfoVO reqVO , HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        if (!OptionalBean.ofNullable(reqVO.getId()).isPresent()) {
            systemParam = systemParamByCacheService.getValues(VcrInfoConstant.VCRINFO_ENUM, VcrInfoConstant.VCRINFO_NO_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        VcrInfo vcrInfo = vcrInfoService.selectByPrimaryKey(reqVO.getId());
        if(null==vcrInfo){
            systemParam = systemParamByCacheService.getValues(VcrInfoConstant.VCRINFO_ENUM, VcrInfoConstant.VCRINFO_NO_DATA_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        try {
            DelFileRequest req = new DelFileRequest();
            req.setBucketName(vcrInfo.getBucketName());
            req.setKey(vcrInfo.getFileName());
            qiniuOssApiService.delOssFile(req);
        }catch (Exception e){
            return setResultSuccess(e.getMessage());
        }
        vcrInfoService.deleteByPrimaryKey(reqVO.getId());
        systemParam = systemParamByCacheService.getValues(VcrInfoConstant.VCRINFO_ENUM, VcrInfoConstant.VCRINFO_DEL_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }
}
